package com.mida.app.report.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.mida.app.report.innerdto.ReportFlowInfo;
import com.mida.app.report.service.CacheService;
import com.mida.app.report.vo.DocReportListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class CacheServiceImpl implements CacheService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    public static final String REPORT_FLOW_INFO_KEY = "reportFlowInfo";

    @Override
    public ReportFlowInfo getReportFlowInfo(Long soid) {
        ReportFlowInfo reportFlowInfo = null;
        String keyName = REPORT_FLOW_INFO_KEY + soid;
        Long status = (Long) redisTemplate.opsForHash().get(keyName, "status");
        if (status == null) {
            return reportFlowInfo;
        }
        reportFlowInfo.setStatus(status);
        Long reportProcessStatus = Long.valueOf(String.valueOf( redisTemplate.opsForHash().get(keyName, "reportProcessStatus")));
        reportFlowInfo.setReportProcessStatus(reportProcessStatus);
        Long firstTrialDoctor = Long.valueOf(String.valueOf( redisTemplate.opsForHash().get(keyName, "firstTrialDoctor")));
        reportFlowInfo.setFirstTrialDoctor(firstTrialDoctor);
        Long firstWriteDoctor = Long.valueOf(String.valueOf( redisTemplate.opsForHash().get(keyName, "firstWriteDoctor")));
        reportFlowInfo.setFirstWriteDoctor(firstWriteDoctor);
        return null;
    }
    @Override
    public boolean exist(Long soid) {
        ReportFlowInfo reportFlowInfo = null;
        String keyName = REPORT_FLOW_INFO_KEY + soid;
//        redisTemplate.delete(keyName);
        if (redisTemplate.opsForHash().get(keyName, "status") == null) {
            return false;
        }
        return true;
    }

    @Override
    public void cacheReportFlowInfo(ReportFlowInfo reportFlowInfo) {
        String keyName = "reportFlowInfo" + reportFlowInfo.getRequestSoid();
        writeKey(keyName, "status", reportFlowInfo.getStatus());
        writeKey(keyName, "reportProcessStatus", reportFlowInfo.getReportProcessStatus());
        writeKey(keyName, "firstTrialDoctorId", reportFlowInfo.getFirstTrialDoctor());
        writeKey(keyName, "firstWriteDoctorId", reportFlowInfo.getFirstWriteDoctor());
        writeKey(keyName, "reportSoid", reportFlowInfo.getReportSoid());
//        writeKey(keyName, "lockStatus", reportFlowInfo.getLockStatus());
//        writeKey(keyName, "lockUserSoid", reportFlowInfo.getLockUserSoid());
//        redisTemplate.opsForHash().put(keyName, "lockUserName",String.valueOf(reportFlowInfo.getLockUserName()));
        redisTemplate.expire(keyName, 1, TimeUnit.DAYS);
//        // FIXME: yutao 2021/8/8 对应的中文名，加一下,下面两个，先写死。以后再说
//        // TODO: yutao 2021/8/8 李建补一下，从对应的status,reportProcessStatus中取到相关的值
        redisTemplate.opsForHash().put(keyName,"statusName",String.valueOf(reportFlowInfo.getStatusName()));
        redisTemplate.opsForHash().put(keyName,"reportStatus",String.valueOf(reportFlowInfo.getReportStatus()));
    }

    private void writeKey(String keyName, String subKeyName, Long status2) {
        redisTemplate.opsForHash().put(keyName, subKeyName, String.valueOf(status2));
    }

    private Object readKey(String keyName, String subKeyName) {
        return redisTemplate.opsForHash().get(keyName, subKeyName);
    }

    private Long readLongKey(String keyName,String subKeyName){
        Object value = readKey(keyName, subKeyName);
        if (ObjectUtils.isEmpty(value)||"null".equals(value)) {
            return null;
        }
        return Long.valueOf((String) readKey(keyName, subKeyName));
    }
       private String readStringKey(String keyName,String subKeyName){
        return (String) readKey(keyName, subKeyName);
    }

    @Override
    public void refreshStatus() {
        // FIXME: yutao 2021/8/8 还没写
    }

    public boolean enabled() {
        // FIXME: yutao 2021/8/8 逻辑没写
        return true;
    }

    @Override
    public void fillStatus(DocReportListVo dv) {
        String keyName = "reportFlowInfo" + dv.getRequestSoid();
        dv.setStatus(readLongKey(keyName,"status"));
        dv.setReportProcessStatus(readLongKey(keyName,"reportProcessStatus"));
        dv.setCheckDoctorSoid(readLongKey(keyName,"firstTrialDoctorId"));
        dv.setWritingDoctorSoid(readLongKey(keyName,"firstWriteDoctorId"));
        dv.setStatusName(readStringKey(keyName,"statusName"));
        dv.setReportStatus(readStringKey(keyName,"reportStatus"));
        dv.setStReportSoid(readLongKey(keyName,"reportSoid"));
    }
}
